<?php

namespace app\admin\controller;

use app\admin\logic\MenuNodeLogic;
use app\common\exception\ApiErrDesc;
use think\App;
use think\facade\Validate;
use think\validate\ValidateRule;


/**
 * 菜单节点控制器
 * Class MenuController
 * @package app\admin\controller
 * @author cenyebao^2_3^
 * @email 1142733640@qq.com
 */
class MenuController extends EndController
{
    protected $notCheckAction = ['adminNav'];

    private $menuNodeLogic;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->menuNodeLogic = new MenuNodeLogic();
    }

    /**
     * 后台菜单
     * @return \think\response\Json
     * @author cenyebao^2_3^
     * @email 1142733640@qq.com
     */
    public function adminNav()
    {
        $params = $this->request->get();

        $roleIds = $this->admin['roleIds'] ?? [];
        $params['role_ids'] = $roleIds;

        $navArr = $this->menuNodeLogic->adminNav( $params );

        $data = [
            // dashboard
            [
                'name' => 'dashboard',
                'parentId' => 0,
                'id' => 1,
                'meta' => [
                    'icon' => 'dashboard',
                    'title' => '仪表盘',
                    'show' => true
                ],
                'component' => 'RouteView',
                'redirect' => '/dashboard/workplace'
            ],
            [
                'name' => 'workplace',
                'parentId' => 1,
                'id' => 7,
                'meta'=> [
                    'title'=> '工作台(^2_3^)',
                    'show'=> true
                ],
                'component'=> 'Workplace'
            ],
            [
                'name' => 'monitor',
                'path' => 'https://www.baidu.com/',
                'parentId' => 1,
                'id' => 3,
                'meta' => [
                    'title' => '监控页（外部）',
                    'target' => '_blank',
                    'show' => true
                ]
            ],
            [
                'name' => 'Analysis',
                'parentId' => 1,
                'id' => 2,
                'meta' => [
                    'title' => '分析页',
                    'show' => true
                ],
                'component' => 'Analysis',
                'path' => '/dashboard/analysis'
            ]
        ];

        return retJson('', 0, $navArr);
    }

    /**
     * 菜单树形结构
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function navTree()
    {
        $params = $this->request->get();
        $treeArr = $this->menuNodeLogic->navTree( $params );
        return retJson('', 0, $treeArr);
    }

    /**
     * 菜单分页列表
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function menuListPage()
    {
        $params = $this->request->get();
        $listPage = $this->menuNodeLogic->menuListPage( $params );
        $listPage = $this->transformPage( $listPage->toArray() );

        return retJson('', 0, $listPage);
    }

    /**
     * 菜单更新
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function edit()
    {
        $params = $this->request->post();
        //// 参数验证
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(),
            'title' => ValidateRule::isRequire(),
            'name' => ValidateRule::isRequire(),
            'pid' => ValidateRule::isInteger(),
        ]);
        if( !$validate->check( $params ) ) {
            return retJson($validate->getError(), ApiErrDesc::ERR_VALIDATE[0]);
        }

        $flag = $this->menuNodeLogic->save( $params );
        if( $flag !== false ) {
            return retJson('', 0, $flag);

        }else {
            return retJson('菜单节点更新失败，稍后再试', ApiErrDesc::ERR_UNKNOWN[0]);
        }
    }

    /**
     * 菜单添加
     * @return \think\response\Json
     * @author ^2_3^王尔贝
     */
    public function add()
    {
        $params = $this->request->post();
        //// 参数验证
        $validate = Validate::rule([
            'title' => ValidateRule::isRequire(),
            'name' => ValidateRule::isRequire(),
            'pid' => ValidateRule::isInteger(),
        ]);
        if( !$validate->check( $params ) ) {
            return retJson($validate->getError(), ApiErrDesc::ERR_VALIDATE[0]);
        }

        $flag = $this->menuNodeLogic->save( $params );
        if( $flag !== false ) {
            return retJson('', 0, $flag);

        }else {
            return retJson('菜单节点失败，稍后再试', ApiErrDesc::ERR_UNKNOWN[0]);
        }
    }
}